//
//  LoginInViewController.swift
//  AnJiBei
//
//  Created by 赵鑫 on 15/10/31.
//  Copyright © 2015年 赵鑫. All rights reserved.
//

import UIKit

class LoginInViewController: BaseViewController {
    
    
    @IBOutlet weak var userPhoneTextField: UITextField! //手机号
    
    @IBOutlet weak var userPasswordTextField: UITextField! //密码
    
    var loginUserInfo = UserLoginModel()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //登陆按钮操作
    @IBAction func loginInButton(sender: UIButton) {
        
        checkUseInfomation()
        
    }
    
    /*********************对登陆的相关判断***************************/
    
    //对文本框检测
    func checkUseInfomation() {
    
        if userPasswordTextField.text!.isEmpty || userPhoneTextField.text!.isEmpty {
        
            let alterView = UIAlertView(title: "提示", message: "不能为空", delegate: self, cancelButtonTitle: "确定")
            
            alterView.show()
        }
        
        else {
        
            //是否为手机号
            if  !RegularExpressionsUtils.regularExpressionPhone(userPhoneTextField.text!) {
            
                let alterView = UIAlertView(title: "提示", message: "输入的手机号不正确，请重新输入", delegate: self, cancelButtonTitle: "确定")
                
                alterView.show()
                
            }
            
            else {
            
                //密码大于6位
                if userPasswordTextField.text!.characters.count < 6 {
                
                    let alterView = UIAlertView(title: "提示", message: "输入的密码至少6位，请重新输入", delegate: self, cancelButtonTitle: "确定")
                    
                    alterView.show()
                    
                }
                
                else {
                
                    requestLogin()
                }
                
            }
            
        }
        
    }
    
    ///保存数据到本地操作
    private func storeDataLocal() {
    
        UserDefaultUtils.setUserSid(loginUserInfo.userSid)
        UserDefaultUtils.setUserTokenId(loginUserInfo.tokenId)
        UserDefaultUtils.setUserPhone(loginUserInfo.userPhone)
        UserDefaultUtils.setUserAvatar(loginUserInfo.userAvatar)
    }
    
    /************************model操作**********************/
    
    func requestLogin () {
        
        let requestLogin = userLoginRequestModel()
        
        requestLogin.userPhone = userPhoneTextField.text!
        requestLogin.userPassword = userPasswordTextField.text!
        
        startRequest(Constant.LOGIN_SYSYTEM_URL, parm: requestLogin)
    }
    
    func requestSuccess(json: AnyObject?, message: String) {
        //
    
        //loginUserInfo = getModel(json)
    
        loginUserInfo = UserLoginModel.jsonToModel(json)
        
        PrintUntils.printLog("\(loginUserInfo.userAvatar)", message: loginUserInfo.userAvatar)
        PrintUntils.printLog("\(loginUserInfo.userName)", message: loginUserInfo.userName)
        PrintUntils.printLog("\(loginUserInfo.tokenId)", message: loginUserInfo.tokenId)
        PrintUntils.printLog("\(loginUserInfo.userSid)", message: loginUserInfo.userSid)

        storeDataLocal()
        self.performSegueWithIdentifier("Login2Main", sender: self)
        
        
    }
    
    func requestDataNull(message: String) {
        //
        disPlayMessage(message)

        
    }
    
    func requestFailure(message: String) {
        //
        disPlayMessage(message)

    }
    
    func requestTimeOut(message: String) {
        //
         disPlayMessage(message)
    }
    
    
    /***********************其他方法****************/

    //键盘消失
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.userPhoneTextField.resignFirstResponder()
        self.userPasswordTextField.resignFirstResponder()
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
